
<template>
  <div>
    <!-- 新增派车单编辑dialog-->
    <el-dialog
      :title="title"
      :close-on-click-modal="false"
      v-dialogDrag
      :visible.sync="editVisible"
      class="tableDialog tableFormDialog"
    >
      <el-form
        :inline="true"
        label-width="95px"
        :rules="dataRule"
        ref="editForm"
        :model="editForm"
        style="position: relative"
      >
        <div class="first-form">
          <el-form-item label="车牌号:" prop="truckNumber" :rules="[
                  {required: true, message:'请选择车辆', trigger:'change'}
                 ]">
            <el-input placeholder="请选择车辆" class="adjustInputAppend" v-model="editForm.truckNumber" @focus="getTruckClient">
<!--              <template slot="append"><span @click="getTruckClient">获取</span></template>-->
              <span slot="suffix"  @click="getTruckClient">获取</span>
            </el-input>
          </el-form-item>

          <el-form-item label="发运计划:" prop="planCode" :rules="[
                  {required: true, message:'请选择发运计划', trigger:'change'}
                 ]">
            <el-input placeholder="请选择发运计划" class="selectInput" v-model="editForm.planCode" @focus="getTransPlanClient">
            </el-input>
          </el-form-item>
        </div>
        <div class="first-form">
          <el-form-item label="司机姓名:" prop="driverName" :rules="[
                  {required: true, message:'请选择司机', trigger:'change'}
                 ]">
            <el-input placeholder="请选择司机" class="selectInput" v-model="editForm.driverName" @focus="getDriverClient">
            </el-input>
          </el-form-item>
          <el-form-item class="origin" label="原发数:" prop="originalWeight" :rules="[
                  {required: true, message:'原发数不能为空', trigger:'blur'},
                  {validator: validator.isNumber, trigger:'blur'}
                 ]">
            <el-input placeholder="请输入原发数" v-thousands.originalWeight="editForm" v-model="editForm.originalWeight">
            </el-input>
          </el-form-item>
        </div>
        <div class="detail">
          <table class="detail-table" cellspacing="0" cellpadding="0">
            <tr class="view-container">
              <td class="label-container">往来名称</td>
              <td class="form-container">{{editForm.companyName}}</td>
              <td class="label-container">数据类别</td>
              <td class="form-container">{{$dictUtils.getDictLabel("dataType", this.editForm.dataTypeTrans, "")}}</td>
            </tr>
            <tr class="view-container">
              <td class="label-container">货物名称</td>
              <td class="form-container">{{editForm.productName}}</td>
              <td class="label-container">司机身份证</td>
              <td class="form-container">{{editForm.driverIdcard}}</td>
            </tr>
            <tr class="view-container">
              <td class="label-container">业务余量</td>
              <td class="form-container">{{editForm.surpQuantity}}</td>
              <td class="label-container">司机电话</td>
              <td class="form-container">{{editForm.driverPhone}}</td>
            </tr>
          </table>
        </div>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button size="small" type="primary" @click="doSubmit()" v-noMoreClick
        >确定</el-button
        >
        <el-button size="small" @click="close()">取消</el-button>
      </span>
    </el-dialog>
    <!--车辆选择弹窗-->
    <TruckDialog ref="TruckDialog" @getTruckData="getTruckData">

    </TruckDialog>
    <!--司机选择弹窗-->
    <DriverDialog ref="DriverDialog" @getDriverData="getDriverData">

    </DriverDialog>
    <!--发运计划选择弹窗-->
    <TransPlanDialog ref="TransPlanDialog" @getTransPlanData="getTransPlanData">

    </TransPlanDialog>
  </div>
</template>
<script>
  import { edit, queryById} from '@/api/transport/transport.js'
  import TruckDialog from '@/components/dialog/truckDialog.vue'
  import DriverDialog from '@/components/dialog/driverDialog.vue'
  import TransPlanDialog from '@/components/dialog/transPlanDialog.vue'
  export default {
    components: {
      TruckDialog,
      DriverDialog,
      TransPlanDialog
    },
    data () {
      return {
        readCardId: '176866578162659321',
        method: '',
        detailsVisible: false,
        title: '',
        editForm: {
          planCode: '', //  发运计划编号
          companyName: '',  //  往来名称
          productName: '', // 货物名称
          surpQuantity: '', //  业务余量
          dataTypeTrans: 0,
          transProps: '', // 派车属性（字典transProps）（1:装货；2:卸货；）
          processType: '',  // 业务流程
          transportPlanId: '',  // 发运计划ID
          transCompanyId: '', // 承运商Id
          truckId: '',  //  车辆ID
          truckNumber: '',  // 车牌号
          driverId: '', // 司机ID
          driverName: '', //  司机姓名
          driverIdcard: '', // 司机身份证号码
          driverPhone: '',  //  司机电话
          driverCredentNumber: '',  //  司机资格证编号
          transMode: '',  // 派车模式（字典transMode）(1:普通派车；2:复合派车)
          truckNumUnexecuted: '', // 可执行车数
          truckNumExecuted: '', // 已执行车数
          originalWeight: '', //  原发/实收数
          originalDate: '', //  原发时间（装货时间）
          effectiveHourTransport: '', // 进厂有效时长(时)装货-进厂
          effectiveHourLoad: '',  //  派车单有效时长(时)接单-装货
          transPriceToDriver: '', //  运费单价2(给司机的运费单价)
          sourceType: '', //  来源类型(1:自己；2:三方物流平台A；3:...)
          thirdPlatDataId: '',  // 第三方物流平台数据ID
          state: '',  //  状态(字典transState)(1:未生效；2:已生效；3:执行中；100:完成；101:作废；)
          truckTrailId: '', //  车辆轨迹主表ID
          id: ''
        },
        readCardForm: {
          cardNumber: '',
          cardInnerNumber: '',
          cardType: '',
          cardTypeValue: '',
          cardState: '',
          cardStateValue: '',
          id: ''
        },
        editVisible: false,
        dialogVisible: false,
        dataRule: {
        }
      }
    },
    methods: {
    	handleClose(done) {
			this.$Msg.confirm({
				title:'您确定要关闭吗？',
				content:this.GLOBAL.FORM_CLOSE_TIPS,
				confirmCall:()=>{
					done();
				}
			})
		},
      init (method, row) {
        if (method === 'add') {
          this.editVisible = true
          this.$nextTick(() => {
            this.$refs.editForm.resetFields()
            this.editForm.companyName = ''
            this.editForm.dataTypeTrans = ''
            this.editForm.productName = ''
            this.editForm.driverIdcard = ''
            this.editForm.surpQuantity = ''
            this.editForm.driverPhone = ''
          })

          this.editForm.id = ''
          this.editForm.addressId = ''
          this.title = '新增派车单'
        } else if (method === 'edit') {
            this.editVisible = true
            this.editForm.id = row
            this.title = '修改派车单'
            this.queryById()
        }
      },
      close () {
        this.editVisible = false
      },
      // 司机选择弹窗
      getDriverClient () {
        this.$refs['DriverDialog'].dialogVisible = true
      },
      // 司机选择数据回显
      getDriverData (row) {
        this.editForm.driverName = row.name
        this.editForm.driverId = row.id
        this.editForm.driverPhone = row.phone
        this.editForm.driverIdcard = row.idcard
        this.editForm.driverCredentNumber = row.credentNumber
        this.dialogVisible = false
      },
      // 发运计划选择弹窗
      getTransPlanClient () {
        this.$refs['TransPlanDialog'].dialogVisible = true
      },
      // 发运计划选择数据回显
      getTransPlanData (row) {
          if (row.business.processType === 1 || row.business.processType === 3 || row.business.processType === 4 || row.business.processType === 5) {
            this.editForm.transProps = 1  // 派车属性（字典transProps）（1:装货）
          }
          if (row.business.processType === 2 || row.business.processType === 6) {
            this.editForm.transProps = 2  // 派车属性（字典transProps）（2:卸货；）
          }
          this.editForm.processType = row.business.processType  // 业务流程
          this.editForm.transportPlanId = row.id  // 发运计划ID
          this.editForm.transCompanyId = '' // 承运商Id
          this.editForm.transMode = ''  // 派车模式（字典transMode）(1:普通派车；2:复合派车)
          this.editForm.truckNumUnexecuted = row.truckNumPlanned // 可执行车数
          this.editForm.truckNumExecuted = row.truckNumExecuted // 已执行车数
          // this.editForm.originalWeight = '' //  原发/实收数
          this.editForm.originalDate = row.sendStartDate //  原发时间（装货时间）
          this.editForm.effectiveHourTransport = row.effectiveHourTransport // 进厂有效时长(时)装货-进厂
          this.editForm.effectiveHourLoad = row.effectiveHourLoad  //  派车单有效时长(时)接单-装货
          this.editForm.transPriceToDriver = row.transPriceToDriver //  运费单价2(给司机的运费单价)
          this.editForm.sourceType = 1 //  来源类型(1:自己；2:三方物流平台A；3:...)
          this.editForm.thirdPlatDataId = ''  // 第三方物流平台数据ID
          this.editForm.state = 0  //  状态(字典transState)(1:未生效；2:已生效；3:执行中；100:完成；101:作废；)
          this.editForm.truckTrailId = '' //  车辆轨迹主表ID
          //  展示
          this.editForm.planCode = row.planCode //  发运计划编号
          this.editForm.companyName = row.business.companyName  // 往来名称
          this.editForm.dataTypeTrans = row.dataType // 数据类别
          this.editForm.productName = row.product.name // 货物名称
          this.editForm.surpQuantity = row.business.surpQuantity // 业务余量
          this.dialogVisible = false
      },
      // 车辆选择弹窗
      getTruckClient () {
        this.$refs['TruckDialog'].dialogVisible = true
      },
      getTruckData (row) {
        this.editForm.truckNumber = row.truckNumber
        this.editForm.truckId = row.id
        this.dialogVisible = false
      },
      queryById () {
          queryById({
              id: this.editForm.id
          }).then(({data}) => {
              if (data.code === '200') {
                  this.editForm.transCode = data.result.transCode
                  this.editForm.transProps = data.result.transProps
                  this.editForm.transportPlanId = data.result.transportPlanId
                  this.editForm.truckTrailId = data.result.truckTrailId
                  this.editForm.truckId = data.result.truckId
                  this.editForm.truckNumber = data.result.truckNumber
                  this.editForm.driverId = data.result.driverId
                  this.editForm.transMode = data.result.transMode
                  this.editForm.truckNumUnexecuted = data.result.truckNumUnexecuted
                  this.editForm.truckNumExecuted = data.result.truckNumExecuted
                  this.editForm.originalWeight = data.result.originalWeight
                  this.editForm.originalTare = data.result.originalTare
                  this.editForm.originalGross = data.result.originalGross
                  this.editForm.ratio = data.result.ratio
                  this.editForm.originalDate = data.result.originalDate
                  this.editForm.effectiveHourTransport = data.result.effectiveHourTransport
                  this.editForm.effectiveHourLoad = data.result.effectiveHourLoad
                  this.editForm.transPriceToDriver = data.result.transPriceToDriver
                  this.editForm.sourceType = data.result.sourceType
                  this.editForm.thirdPlatDataId = data.result.thirdPlatDataId
                  this.editForm.state = data.result.state
                  this.editForm.planCode = data.result.transportPlan.planCode
                  this.editForm.driverName = data.result.truckDriver.name
                  this.editForm.companyName = data.result.business.companyName
                  this.editForm.productName = data.result.product.name
                  this.editForm.driverPhone = data.result.truckDriver.phone
                  this.editForm.driverIdcard = data.result.truckDriver.idcard
                  this.editForm.surpQuantity = data.result.business.surpMoney // 业务余量
              }
          })
      },
      doSubmit () {
        this.$refs['editForm'].validate((valid) => {
          if (valid) {
            edit(this.editForm).then(({data}) => {
              if (data.code === '200') {
                this.$message({
                  message: '保存成功',
                  type: 'success'
                })
                this.editVisible = false
                this.$emit('refreshDataList')
              }
            })
          }
        })
      }
    }
  }
</script>
<style lang="scss" scoped>
  @import '@/assets/scss/config.scss';
  ::v-deep.first-form{
    .el-input{
      .el-input__inner{
        width: 241px;
      }
    }
    .adjustInputAppend.el-input{
      .el-input__suffix{
        right: 0;
        padding:0 8px 0 5px;
        cursor: pointer;
        color: $--color-primary-liyu;
      }
    }
  }
  .first-form{
    .el-form-item:first-child{
      margin-right: 23px !important;
    }
  }
  .readBut {
    width: 93px;
    height: 50px;
    background: #1890ff;
    border-radius: 5px;
    position: absolute;
    right: 40px;
    top: 20px;
    font-size: 23px;
    font-weight: 400;
    color: #ffffff;
    text-align: center;
    padding-top: 12px;
    cursor:pointer;
  }
  .detail-table{
    .label-container{
      width: 95px;
    }
    .form-container{

    }
  }
</style>
